package com.example.demo.service.Impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.common.Result;
import com.example.demo.dto.Gkpaper.GkpaperAddRequest;
import com.example.demo.dto.Gkpaper.GkpaperQueryRequest;
import com.example.demo.entity.Gkhomework;
import com.example.demo.entity.Gkpaper;
import com.example.demo.exception.CustomException;
import com.example.demo.service.GkpaperService;
import com.example.demo.dao.GkpaperMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
* @author darling
* @description 针对表【gkpaper】的数据库操作Service实现
*
*/
@Service
public class GkpaperServiceImpl extends ServiceImpl<GkpaperMapper, Gkpaper>
    implements GkpaperService{

    @Override
    public QueryWrapper<Gkpaper> getQueryWrapper(GkpaperQueryRequest gkpaperQueryRequest) {
        QueryWrapper<Gkpaper> queryWrapper = new QueryWrapper<>();
        if (gkpaperQueryRequest == null) {
            return queryWrapper;
        }
        // 从对象中取值
        Integer id = gkpaperQueryRequest.getId();
        String name = gkpaperQueryRequest.getName();
        Integer price = gkpaperQueryRequest.getPrice();
        Integer count = gkpaperQueryRequest.getCount();
        Integer countprice = gkpaperQueryRequest.getCountprice();
        String file = gkpaperQueryRequest.getFile();
        String sortField = gkpaperQueryRequest.getSortField();
        String sortOrder = gkpaperQueryRequest.getSortOrder();

        // 模糊查询
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        // 精确查询
        queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id);
        queryWrapper.eq(ObjectUtils.isNotEmpty(price), "price", price);
        queryWrapper.eq(ObjectUtils.isNotEmpty(count), "count", count);
        queryWrapper.eq(ObjectUtils.isNotEmpty(countprice), "countprice", countprice);
        queryWrapper.eq(ObjectUtils.isNotEmpty(file), "file", file);

        // 排序规则
        queryWrapper.orderBy(StrUtil.isNotEmpty(sortField), sortOrder.equals("ascend"), sortField);
        return queryWrapper;
    }

    @Override
    public boolean addgkpaper(MultipartFile file, GkpaperAddRequest gkpaperAddRequest) {
        if (file.isEmpty()) {
            throw new CustomException("上传文件不能为空");
        }
        // 原始文件名
        String originalFilename = file.getOriginalFilename();
        // 获取文件后缀
        String extname = "";
        int index = originalFilename.lastIndexOf(".");
        if (index > 0) {
            extname = originalFilename.substring(index);
        }
        // 生成新文件名 80cc79ad-9956-4bee-b979-160513143a45.doc
        String newFileName = UUID.randomUUID() + extname;
        // 构建保存路径
        String currentPath = System.getProperty("user.dir");//C:\Users\darling\Desktop\xx\demo
        String saveDir = currentPath + File.separator + "gkpaper";//C:\Users\darling\Desktop\xx\demo\gkpaper
        String fullPath = saveDir + File.separator + newFileName; // C:\Users\darling\Desktop\xx\demo\gkpaper\80cc79ad-9956-4bee-b979-160513143a45.doc
            // 创建目录（如果不存在）
            File dir = new File(saveDir);
            if (!dir.exists()) {
                boolean created = dir.mkdirs();
                if (!created) {
                    throw new CustomException("创建目录失败: " + saveDir);
                }
            }
        try {
            // 保存文件
            file.transferTo(new File(fullPath));
        } catch (IOException e) {
            e.printStackTrace();
            throw new CustomException("文件保存失败: " + e.getMessage());
        }
            Gkpaper gkpaper = new Gkpaper();
            BeanUtils.copyProperties(gkpaperAddRequest, gkpaper);
            //将newFileName变成/
            gkpaper.setFile("/gkpaper/"+newFileName);
            boolean result = this.save(gkpaper);
            if (!result){
                throw new CustomException("添加失败");
            }
            return true;
    }
}




